package com.naizhuai.ui.task.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.naizhuai.R;
import com.naizhuai.ui.components.CircleImageView;
import com.naizhuai.ui.entity.UserLeader;
import com.naizhuai.util.ViewUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author：lichenxi
 * @date 2018/6/6 16
 * email：525603977@qq.com
 * Fighting
 */
public class FlowSelectLoader extends RecyclerView.Adapter<FlowSelectLoader.ViewHolder> {

    private List<UserLeader> managerList;
    private String selectUid;

    public String getSelectUid() {
        return selectUid;
    }

    public FlowSelectLoader(List<UserLeader> managerList) {
        this.managerList = managerList;
    }

    private int selectposition = -1;

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View root = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_flow_selectleader, parent, false);
        return new ViewHolder(root);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        UserLeader userLeader = managerList.get(holder.getAdapterPosition());
        if (selectposition == holder.getAdapterPosition()) {
            holder.cbImageCheck.setVisibility(View.VISIBLE);
        } else {
            holder.cbImageCheck.setVisibility(View.INVISIBLE);
        }
        ViewUtils.showAvatorOrName(userLeader.thumb, userLeader.username, holder.imageView);
        holder.name.setText(userLeader.username);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectposition = position;
                selectUid = userLeader.uid;
                notifyDataSetChanged();
            }
        });
    }

    @Override
    public int getItemCount() {
        return managerList != null ? managerList.size() : 0;
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.imageView)
        CircleImageView imageView;
        @BindView(R.id.cb_image_check)
        ImageView cbImageCheck;
        @BindView(R.id.name)
        TextView name;
        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

        }
    }
}
